package com.cskaoyan.shopping.service.impl;

import com.cskaoyan.mall.dto.ItemDto;
import com.cskaoyan.mall.dto.ProductDetailDto;
import com.cskaoyan.shopping.converter.ProductConverter;
import com.cskaoyan.shopping.dal.entitys.Item;
import com.cskaoyan.shopping.dal.entitys.ItemDesc;
import com.cskaoyan.shopping.dal.persistence.ItemDescMapper;
import com.cskaoyan.shopping.dal.persistence.ItemMapper;
import com.cskaoyan.mall.dto.ProductDto;
import com.cskaoyan.shopping.converter.ContentConverter;
import com.cskaoyan.shopping.dal.entitys.Panel;
import com.cskaoyan.shopping.dal.entitys.PanelContentItem;
import com.cskaoyan.shopping.dal.persistence.PanelContentMapper;
import com.cskaoyan.shopping.dal.persistence.PanelMapper;
import com.cskaoyan.shopping.dto.AllProductResponse;
import com.cskaoyan.shopping.dto.PanelContentItemDto;
import com.cskaoyan.shopping.dto.PanelDto;
import com.cskaoyan.shopping.form.PageRequest;
import com.cskaoyan.shopping.service.IProductService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jdk.nashorn.internal.ir.ReturnNode;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class IProductServiceImpl implements IProductService {


    @Autowired
    ItemMapper itemMapper;

    @Autowired
    ItemDescMapper itemDescMapper;

    @Autowired
    ProductConverter productConverter;
    
    @Autowired
    PanelMapper panelMapper;

    @Autowired
    ContentConverter contentConverter;

    @Autowired
    PanelContentMapper panelContentMapper;


    @Override
    public ProductDetailDto getProductDetail(Long id) {

        Item item = itemMapper.selectByPrimaryKey(id);

        if (item == null) {
            return null;
        }
        ItemDesc itemDesc = itemDescMapper.selectByPrimaryKey(id);

        ProductDetailDto productDetailDto = productConverter.toProductDetailDto(item, itemDesc);
        return productDetailDto;
    }

    @Override
    public AllProductResponse getAllProduct(PageRequest pageInfo) {
        AllProductResponse allProductResponse = new AllProductResponse();
        //获取所有的商品
        PageHelper.startPage(pageInfo.getPage(),pageInfo.getSize());
        Example example = new Example(Item.class);
        //TODO 按照价格排序  1的时候按照价格从低到到
        String sort = pageInfo.getSort();
        if (!StringUtils.isEmpty(sort)) {
            if (Integer.parseInt(pageInfo.getSort()) == 1) {
                example.setOrderByClause("price asc");
            } else if (Integer.parseInt(pageInfo.getSort()) == -1) {
                example.setOrderByClause("price desc");
            }
        } else {
            example.setOrderByClause("created desc");
        }
        //TODO 2 按照价格最低最高值查找
        Example.Criteria criteria = example.createCriteria();
        if (pageInfo.getPriceGt() != null) {
            criteria.andGreaterThan("price",pageInfo.getPriceGt());
        }
        if (pageInfo.getPriceLte() != null) {
            criteria.andLessThanOrEqualTo("price",pageInfo.getPriceLte());
        }
        if (pageInfo.getCid() != null) {
            criteria.andEqualTo("cid",pageInfo.getCid());
        }

        List<Item> items = itemMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(items)) {
            allProductResponse.setData(new ArrayList<>());
            allProductResponse.setTotal(0L);
            return allProductResponse;
        }
        PageInfo<Item> itemPageInfo = new PageInfo<>(items);
        long total = itemPageInfo.getTotal();
        List<ProductDto> productDtos = productConverter.items2Dto(items);
        allProductResponse.setData(productDtos);
        allProductResponse.setTotal(total);
        return allProductResponse;
    }


    /**
    * @description: 商品推荐的模块
    []
    * @return: java.util.Set<com.cskaoyan.shopping.dto.PanelDto>
    * @author: 杨庆
    * @time: 2022.9.21 10:37
     */
    @Override
    public Set<PanelDto> getRecommendGoods() {
        Set<PanelDto> panelDtos = new HashSet<>();
        Example example = new Example(Panel.class);
        example.createCriteria()
                .andEqualTo("remark","热门推荐");
        List<Panel> panels = panelMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(panels)) {
            return panelDtos;
        }
        Panel panel = panels.get(0);
        PanelDto panelDto = contentConverter.panel2Dto(panel);
        //TODO 根据板块的id
        List<PanelContentItem> panelContentItems = panelContentMapper.selectPanelContentAndProductWithPanelId(panel.getId());
        List<PanelContentItemDto> panelContentItemDtos = contentConverter.panelContentItem2Dto(panelContentItems);
        panelDto.setPanelContentItems(panelContentItemDtos);
        panelDtos.add(panelDto);
        return panelDtos;
    }

    @Override
    public List<ItemDto> getAllItems() {
        return null;
    }

}
